package com.example.myapplication_mutiactivity

import android.content.res.Configuration
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity4 : AppCompatActivity() {
    private lateinit var tvTimer: TextView
    private lateinit var btnStartPause: Button
    private lateinit var btnReset: Button
    private lateinit var btnBack: Button

    private var seconds = 0
    private var isRunning = false
    private val handler = Handler(Looper.getMainLooper())
    private lateinit var timerRunnable: Runnable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main4)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initViews()
        initTimerRunnable()
        setButtonListeners()
        updateTimerUI()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        setContentView(R.layout.activity_main4)
        initViews()
        setButtonListeners()
        updateTimerUI()
        if (isRunning) {
            startTimer()
        }
    }

    private fun initViews() {
        tvTimer = findViewById(R.id.tv_timer)
        btnStartPause = findViewById(R.id.btn_start_pause)
        btnReset = findViewById(R.id.btn_reset)
        btnBack = findViewById(R.id.btn_back)
    }

    private fun initTimerRunnable() {
        timerRunnable = object : Runnable {
            override fun run() {
                if (isRunning) {
                    seconds++
                    updateTimerUI()
                    handler.postDelayed(this, 1000)
                }
            }
        }
    }

    private fun setButtonListeners() {
        btnStartPause.setOnClickListener {
            isRunning = !isRunning
            if (isRunning) {
                btnStartPause.text = "暂停"
                startTimer()
            } else {
                btnStartPause.text = "开始"
                handler.removeCallbacks(timerRunnable)
            }
        }

        btnReset.setOnClickListener {
            isRunning = false
            handler.removeCallbacks(timerRunnable)
            seconds = 0
            updateTimerUI()
            btnStartPause.text = "开始"
        }

        btnBack.setOnClickListener {
            finish()
        }
    }

    private fun startTimer() {
        handler.post(timerRunnable)
    }

    private fun updateTimerUI() {
        val hours = seconds / 3600
        val minutes = (seconds % 3600) / 60
        val secs = seconds % 60
        tvTimer.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(timerRunnable)
    }
}